package com.rtx.edi.platform.department.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.department.dto.PlDepartmentDto;
import com.rtx.edi.model.department.po.PlDepartment;
import com.rtx.edi.platform.department.mapper.PlDepartmentMapper;
import com.rtx.edi.platform.department.service.PlDepartmentService;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 部门信息表  服务实现类
 * </p>
 *
 * @author abc
 * @since 2023-03-05
 */
@Service
public class PlDepartmentServiceImpl extends ServiceImpl<PlDepartmentMapper, PlDepartment> implements PlDepartmentService {

    @Override
    public void selectPlDepartmentList(Page<PlDepartment> pageParam, PlDepartment plDepartment) {
        LambdaQueryWrapper<PlDepartment> query = Wrappers.lambdaQuery();
        query.orderByAsc(PlDepartment::getDeptNo);
        if(plDepartment != null){
            query
                    .eq(StringUtils.hasText(plDepartment.getDeptNo()),PlDepartment::getDeptNo,plDepartment.getDeptNo())
                    .like(StringUtils.hasText(plDepartment.getDeptName()),PlDepartment::getDeptName,plDepartment.getDeptName());
        }
        baseMapper.selectPage(pageParam,query);
    }

    @Override
    public boolean addPlDepartment(PlDepartment plDepartment) {
        //验证非空字段
        checkField(plDepartment);
        //验证部门编号是否存在
        LambdaQueryWrapper<PlDepartment> query1 = Wrappers.lambdaQuery();
        query1.eq(PlDepartment::getDeptNo,plDepartment.getDeptNo());
        Assert.isTrue(!baseMapper.exists(query1),"部门编号已存在");
        //验证部门名称是否存在
        LambdaQueryWrapper<PlDepartment> query2 = Wrappers.lambdaQuery();
        query2.eq(PlDepartment::getDeptName,plDepartment.getDeptName());
        Assert.isTrue(!baseMapper.exists(query2),"部门名称已存在");
        return baseMapper.insert(plDepartment) == 1;
    }

    /**
     * 验证非空字段
     * @param plDepartment
     */
    private void checkField(PlDepartment plDepartment) {
        Assert.notNull(plDepartment.getCompanyId(),"公司id不能为空");
        Assert.hasText(plDepartment.getDeptNo(),"部门编号不能为空");
        Assert.hasText(plDepartment.getDeptName(),"部门名称不能为空");
    }

    /**
     * 批量删除
     */
	public void deletePlApplicationByIds(List<PlDepartmentDto> list) {
		for(PlDepartmentDto dto:list) {
			PlDepartment po = new PlDepartment();
			po.setId(dto.getId());
			po.setIsDeleted(1);
			baseMapper.updateById(po);
		}
	}
    
    
}
